package 数据结构下

import scala.collection.mutable

object Exercise04 {

  def main(args: Array[String]): Unit = {
    val sentence = "AAAAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDD"
    val value = sentence.foldLeft(Map[Char, Int]())(charCount)
    println(value)
    val mutableMap = mutable.Map[Char, Int]()
    sentence.foldLeft(mutableMap)(charCount2)
    println(mutableMap)
  }

  //不可变map 有序的
  def charCount(map: Map[Char, Int], c: Char): Map[Char, Int] = {
    map + (c -> (map.getOrElse(c, 0) + 1))
  }

  //可变map 无序的
  def charCount2(map: mutable.Map[Char, Int], c: Char): mutable.Map[Char, Int] = {
    map += (c -> (map.getOrElse(c, 0) + 1))
  }
}
